#include "Position.h"

Position::Position()
{
}

bool Position::isOverChessBoard(const int y, const int x)
{
    if(y > 24 && y < 24 + 77 * 8 && x > 24 && x < 24 + 77 * 8) return true;

    return false;
}

QString Position::index2position(const int y, const int x)
{
    QChar chars[] = { 'a' ,'b', 'c', 'd', 'e', 'f', 'g', 'h' };
    QChar letter = chars[x];
    QChar number = QString::number(8 - y).at(0);

    QString result;
    result.append(letter);
    result.append(number);

    return result;
}

int * Position::position2index(const QString &position, int result[2])
{
    QChar chars[] = { 'a' ,'b', 'c', 'd', 'e', 'f', 'g', 'h' };
    QChar letter = position.at(0);
    QChar number = position.at(1);

    for(int i = 0; i < 8; ++i) if(letter == chars[i]) { result[1] = i; break; }

    result[0] = 8 - number.digitValue();

    return result;
}

int * Position::coords2index(const int y, const int x, int result[2])
{
    int beginX = 24;
    int beginY = beginX;
    int space  = 77;
    int tx, ty;

    for(int i = 0; i <= 7; ++i) {
        if(x >= beginX + space * i && x <= beginX + space * (i + 1)) {
            tx = i;
            break;
        }
    }

    for(int i = 0; i <= 7; ++i) {
        if(y >= beginY + space * i && y <= beginY + space * (i + 1)) {
            ty = i;
            break;
        }
    }

    result[0] = ty;
    result[1] = tx;

    return result;
}
